const chooseLocation = requirePlugin('chooseLocation')

import WxValidate from '../../../../assets/plugins/wx-validate/WxValidate'
import Dialog from '@vant/weapp/dialog/dialog';

var App = getApp();

Page({
    data: {
        item: {
            name: '',
            phone: '',
            detail: '',
            province: '',
            city: '',
            detail_lat: '',
            detail_lng: '',
            default: true
        },
    },

    onLoad: function (options) {
        this.initValidate()
        if (options.item) {
            var item = JSON.parse(options.item)
            item.default = item.default == 2
            this.setData({
                item: item
            })
        }
    },

    /**
     * 初始化验证信息
     */
    initValidate() {
        // 验证字段的规则
        const rules = {
            name: {
                required: true,
            },
            phone: {
                required: true,
            },
            detail: {
                required: true,
            },
            detail_lat: {
                required: true,
            },
            detail_lng: {
                required: true,
            }

        }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = {
            name: {
                required: '请填写联系人姓名'
            },
            phone: {
                required: '请填写手机号码'
            },
            detail: {
                required: '请填写详细地址',
            },
            detail_lat: {
                required: '请选择地址',
            },
            detail_lng: {
                required: '请选择地址',
            },
        }
        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages)
    },

    // 从地图选点插件返回后，在页面的onShow生命周期函数中能够调用插件接口，取得选点结果对象
    onShow() {
        let location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
        if (location) {
            this.setData({
                "item.city": location.name,
                "item.province": location.address,
                "item.detail_lng": location.longitude,
                "item.detail_lat": location.latitude
            })
        }
    },

    getPhoneNumber: function (e) {
        const _this = this
        wx.login({
            success: res => {
                if (res.code) {
                    App.globalData.userModel.mobilePhone({
                        data: {
                            code: res.code,
                            iv: e.detail.iv,
                            encryptedData: e.detail.encryptedData
                        }
                    }).then(json => {
                        _this.setData({
                            "item.phone": json.data.phoneNumber
                        })
                    })
                }
            }
        })
    },

    onChange() {
        this.setData({
            "item.default": !this.data.item.default
        })
    },

    submitForm(e) {
        const _this = this
        const params = Object.assign(
            _this.data.item, e.detail.value
        )

        // 传入表单数据，调用验证方法
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0]
            _this.setData({
                isPop: true
            })
            Dialog.alert({
                message: error.msg,
                width: '560rpx',
            }).then(() => {
                _this.setData({
                    isPop: false
                })
            });
            return false
        }
        if (!_this.data.item.id) {
            App.globalData.userModel.addAdd({
                data: params
            }).then(json => {
                Dialog.alert({
                    message: json.data,
                    width: '560rpx',
                }).then(() => {
                    wx.navigateBack();
                });

            })
        } else {
            App.globalData.userModel.editAdd({
                data: params
            }).then(json => {
                Dialog.alert({
                    message: json.data,
                    width: '560rpx',
                }).then(() => {
                    wx.navigateBack();
                });

            })
        }

    }
})